export class HtmlBuilder {
  private elements: string[] = [];
  private indentSize: number = 2;
  private indentLevel: number = 0;
  
  /**
   * 设置缩进大小
   * @param size 缩进空格数
   * @returns 当前构建器实例
   */
  public setIndentSize(size: number): HtmlBuilder {
    this.indentSize = size;
    return this;
  }
  
  /**
   * 获取当前缩进空格
   * @returns 缩进空格字符串
   */
  private getIndent(): string {
    return ' '.repeat(this.indentSize * this.indentLevel);
  }
  
  /**
   * 添加HTML注释
   * @param comment 注释内容
   * @returns 当前构建器实例
   */
  public addComment(comment: string): HtmlBuilder {
    this.elements.push(`${this.getIndent()}<!-- ${comment} -->`);
    return this;
  }
  
  /**
   * 添加HTML标签
   * @param tag 标签名
   * @param content 内容
   * @param attributes 属性对象
   * @returns 当前构建器实例
   */
  public addTag(tag: string, content: string = '', attributes: Record<string, string> = {}): HtmlBuilder {
    const attrs = Object.entries(attributes)
      .map(([key, value]) => `${key}="${value}"`)
      .join(' ');
    
    const attrStr = attrs ? ` ${attrs}` : '';
    
    if (content) {
      this.elements.push(`${this.getIndent()}<${tag}${attrStr}>${content}</${tag}>`);
    } else {
      this.elements.push(`${this.getIndent()}<${tag}${attrStr}></${tag}>`);
    }
    
    return this;
  }
  
  /**
   * 添加自关闭标签
   * @param tag 标签名
   * @param attributes 属性对象
   * @returns 当前构建器实例
   */
  public addSelfClosingTag(tag: string, attributes: Record<string, string> = {}): HtmlBuilder {
    const attrs = Object.entries(attributes)
      .map(([key, value]) => `${key}="${value}"`)
      .join(' ');
    
    const attrStr = attrs ? ` ${attrs}` : '';
    
    this.elements.push(`${this.getIndent()}<${tag}${attrStr} />`);
    
    return this;
  }
  
  /**
   * 开始添加带有子元素的标签
   * @param tag 标签名
   * @param attributes 属性对象
   * @returns 当前构建器实例
   */
  public startTag(tag: string, attributes: Record<string, string> = {}): HtmlBuilder {
    const attrs = Object.entries(attributes)
      .map(([key, value]) => `${key}="${value}"`)
      .join(' ');
    
    const attrStr = attrs ? ` ${attrs}` : '';
    
    this.elements.push(`${this.getIndent()}<${tag}${attrStr}>`);
    this.indentLevel++;
    
    return this;
  }
  
  /**
   * 结束当前标签
   * @param tag 标签名
   * @returns 当前构建器实例
   */
  public endTag(tag: string): HtmlBuilder {
    this.indentLevel = Math.max(0, this.indentLevel - 1);
    this.elements.push(`${this.getIndent()}</${tag}>`);
    
    return this;
  }
  
  /**
   * 添加原始HTML文本
   * @param html HTML文本
   * @returns 当前构建器实例
   */
  public addRaw(html: string): HtmlBuilder {
    this.elements.push(`${this.getIndent()}${html}`);
    return this;
  }
  
  /**
   * 添加换行符
   * @returns 当前构建器实例
   */
  public addBreak(): HtmlBuilder {
    this.elements.push('');
    return this;
  }
  
  /**
   * 添加简单的CSS样式标签
   * @param css CSS内容
   * @returns 当前构建器实例
   */
  public addStyle(css: string): HtmlBuilder {
    this.startTag('style');
    this.addRaw(css);
    this.endTag('style');
    return this;
  }
  
  /**
   * 添加外部脚本
   * @param src 脚本URL
   * @returns 当前构建器实例
   */
  public addScript(src: string): HtmlBuilder {
    this.addTag('script', '', { src });
    return this;
  }
  
  /**
   * 添加内联脚本
   * @param js JavaScript代码
   * @returns 当前构建器实例
   */
  public addInlineScript(js: string): HtmlBuilder {
    this.startTag('script');
    this.addRaw(js);
    this.endTag('script');
    return this;
  }
  
  /**
   * 构建完整HTML文档
   * @param title 文档标题
   * @param lang 语言，默认为zh-CN
   * @returns 当前构建器实例
   */
  public buildDocument(title: string, lang: string = 'zh-CN'): HtmlBuilder {
    this.elements = [];
    this.indentLevel = 0;
    
    this.addRaw('<!DOCTYPE html>');
    this.startTag('html', { lang });
    
    // 添加头部
    this.startTag('head');
    this.addTag('meta', '', { charset: 'UTF-8' });
    this.addTag('meta', '', { name: 'viewport', content: 'width=device-width, initial-scale=1.0' });
    this.addTag('title', title);
    this.endTag('head');
    
    // 添加主体
    this.startTag('body');
    this.indentLevel++;
    
    return this;
  }
  
  /**
   * 完成HTML文档构建
   * @returns 当前构建器实例
   */
  public finishDocument(): HtmlBuilder {
    this.indentLevel = Math.max(0, this.indentLevel - 1);
    this.endTag('body');
    this.endTag('html');
    return this;
  }
  
  /**
   * 将构建的HTML转为字符串
   * @returns HTML字符串
   */
  public toString(): string {
    return this.elements.join('\n');
  }
}
